<?php

require_once ('class.phpmailer.php');

class SkillSendMail {

    public static function SendMailUser($mailArr, $tplReplaceArr = array(), $body = NULL) {
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->SMTPSecure = "tls";
        $mail->Host = EMAIL_HOST;
        $mail->Username = EMAIL_USER_NAME;
        $mail->Password = EMAIL_PASSWORD;
        $mail->SetFrom(EMAIL_FROM, 'SkillHippo');
        $mail->Subject = $mailArr['subject'];
        $body = '';
        $msgTpl = $mailArr['msgtpl'];
        if (file_exists($msgTpl)) {
            $body = file_get_contents($msgTpl, true);
        }
        foreach ($tplReplaceArr as $key => $value) {
            $body = str_replace($key, $value, $body);
        }
        $mail->ContentType = 'text/plain';
        $mail->IsHTML(true); // send as HTML
        $mail->Body = $body;
        if (array_key_exists('to', $mailArr)) {
            $toArr = explode(',', $mailArr['to']);
            if (!empty($toArr)) {
                foreach ($toArr as $to) {
                    $mail->AddAddress($to);
                }
            }
        }

        if (array_key_exists('cc', $mailArr)) {
            $ccArr = explode(',', $mailArr['cc']);
            if (!empty($ccArr)) {
                foreach ($ccArr as $cc) {
                    $mail->AddBCC($cc);
                }
            }
        }

        if (array_key_exists('bcc', $mailArr)) {
            $bccArr = explode(',', $mailArr['bcc']);
            if (!empty($bccArr)) {
                foreach ($bccArr as $bcc) {
                    $mail->AddBCC($bcc);
                }
            }
        }
        $mail->Send();
    }

    public static function SendMail($to, $subject, $body) {
        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->SMTPSecure = "tls";
        $mail->Host = EMAIL_HOST;
        $mail->Username = EMAIL_USER_NAME;
        $mail->Password = EMAIL_PASSWORD;
        $mail->SetFrom(EMAIL_FROM, 'SkillHippo');
        $mail->Subject = $subject;

        try {
            $mail->MsgHTML($body);
            $mail->IsHTML(false); // send as HTML
            $mail->AddAddress($to);
            $mail->Send();
        } catch (Exception $e) {
            
        }
    }

    public static function SendMailCreateCLassCourse($to, $subject, $body) {
        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->SMTPSecure = "tls";
        $mail->Host = EMAIL_HOST;
        $mail->Username = EMAIL_USER_NAME;
        $mail->Password = EMAIL_PASSWORD;
        $mail->SetFrom(EMAIL_FROM, '');
        $mail->Subject = $subject;

        try {
            $mail->MsgHTML($body);
            $mail->IsHTML(false); // send as HTML
            $mail->AddAddress($to);
            $mail->AddBCC(EMAIL_TO_ADMIN);
            $mail->Send();
        } catch (Exception $e) {
            
        }
    }

    public static function CustomeSendMail($to, $mailfrom, $subject, $body) {
        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->SMTPSecure = "ssl";
        $mail->Host = EMAIL_HOST;
        $mail->Username = EMAIL_USER_NAME;
        $mail->Password = EMAIL_PASSWORD;
        $mail->SetFrom($mailfrom);
        $mail->Subject = $subject;

        try {
            $mail->MsgHTML($body);
            $mail->IsHTML(false); // send as HTML
            $mail->AddAddress($to);

            $mail->Send();
        } catch (Exception $e) {
            
        }
    }

}

?>